package com.smartstate.dataManagement.util;

import com.alibaba.fastjson.JSON;
import com.smartstate.dataManagement.dto.EnergyDTO;
import com.smartstate.dataManagement.entity.ObjMoudel;
import com.smartstate.dataManagement.service.ObjMoudelService;
import com.smartstate.dataManagement.service.impl.ObjMoudelServiceImpl;
import com.smartstate.dataManagement.vo.ObjMoudelTwoVo;
import com.smartstate.dataManagement.vo.PowerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 * <p>@Author 陈永杰</p>
 * <p>@Date: 2022/9/5</p>
 * <p>@Time: 9:44</p>
 * <p>@version 1.0 <p>
 */
public class PowerMeterSummaryUtils {


    public List<Map<String, Object>>  PowerTree(Map<String, List<PowerVo>> collectPower,EnergyDTO energyDTO){
        //注入bean
        ObjMoudelService bean = SpringUtils.getBean(ObjMoudelService.class);
        List<Map<String, Object>> listAll = new ArrayList<>();
        //拿到所有时间段的能耗值以及id 和 parentId
        for (Map.Entry<String, List<PowerVo>> stringListEntry : collectPower.entrySet()) {
            //拿到注入bean的树的方法
            Map<String, Object> mapAllTree = bean.selectPowerTree(energyDTO);
            List<PowerVo> value = stringListEntry.getValue();
            for (PowerVo powerVo : value) {
                if (powerVo.getRateType()!=null && !powerVo.getRateType().equals("")) {
                    Tree(mapAllTree, powerVo.getId(), powerVo.getNum(), powerVo.getPrice(),powerVo.getRate(),powerVo.getPeekValue(),powerVo.getFlatValue(),powerVo.getValleyValue(),powerVo.getTime(), powerVo.getRateType(), powerVo.getGraininess());
                }else{
                    Tree(mapAllTree, powerVo.getId(), "0.00","0.00","0.00" ,"0.00","0.00","0.00",powerVo.getTime(), "暂未配置计算公式", powerVo.getGraininess());
                }
            }
            listAll.add(mapAllTree);
        }
        return  listAll;
    }
    public void  Tree(Map<String,Object> map, String id, String num,String price, String rate,String PeekValue,String FlatValue,String ValleyValue,String time,String rateType,String graininess){
        String s =  (String) map.get("id");
        if (s.equals(id)){
            map.put("num", num);
            map.put("price", price.toString());
            map.put("rate", rate.toString());
            map.put("PeekValue", PeekValue.toString());
            map.put("FlatValue", FlatValue.toString());
            map.put("ValleyValue", ValleyValue.toString());
            map.put("time",time);
//            map.put("carbon", carbon);

            map.put("rateType", rateType);
            map.put("graininess", graininess);
        }else if(!s.equals(id) && !map.containsKey("num")){
            map.put("num","0.00");
            map.put("price", "0.00");
            map.put("rate", "0.00");
            map.put("carbon", "0.00");
            map.put("PeekValue","0.00");
            map.put("FlatValue", "0.00");
            map.put("ValleyValue","0.00");
            map.put("time",time);
            map.put("rateType", rateType);
            map.put("graininess", graininess);
        }else if (s.equals(id) && rateType==null && rateType.equals("")){
            map.put("num",num);
            map.put("price", price);
            map.put("rate", rate);
            map.put("PeekValue", PeekValue);
            map.put("FlatValue", FlatValue);
            map.put("ValleyValue", ValleyValue);
//            map.put("carbon", carbon);
            map.put("time",time);
            map.put("rateType", rateType);
            map.put("graininess", graininess);
        }
        //能耗
        if (s.equals(id)){
            if (map.containsKey("num") || map.get("num").equals(0)){
                map.replace("num",num.toString());
                map.replace("time",time);
                map.put("rateType", rateType);
                map.put("graininess", graininess);
            }else {
                map.put("num", num.toString());
                map.put("time", time);
                map.put("rateType", rateType);
                map.put("graininess", graininess);
            }
        }

        //成本
        if (s.equals(id)){
            if (map.containsKey("price") || map.get("price").equals(0)){
                map.replace("price",price.toString());
                map.replace("time",time);
                map.put("rateType", rateType);
                map.put("graininess", graininess);
            }else {
                map.put("price", price.toString());
                map.put("time", time);
                map.put("rateType", rateType);
                map.put("graininess", graininess);
            }
        }
        //平均值
        if (s.equals(id)){
            if (map.containsKey("rate") || map.get("rate").equals(0)){
                map.replace("rate",rate.toString());
                map.replace("time",time);
                map.put("rateType", rateType);
                map.put("graininess", graininess);
            }else {
                map.put("rate", rate.toString());
                map.put("time", time);
                map.put("rateType", rateType);
                map.put("graininess", graininess);
            }
        }

        //峰
        if (s.equals(id)){
            if (map.containsKey("PeekValue") || map.get("PeekValue").equals(0)){
                map.replace("PeekValue",PeekValue.toString());
                map.replace("time",time);
                map.put("rateType", rateType);
                map.put("graininess", graininess);
            }else {
                map.put("PeekValue", PeekValue.toString());
                map.put("time", time);
                map.put("rateType", rateType);
                map.put("graininess", graininess);
            }
        }
        //平
        if (s.equals(id)){
            if (map.containsKey("FlatValue") || map.get("FlatValue").equals(0)){
                map.replace("FlatValue",FlatValue.toString());
                map.replace("time",time);
                map.put("rateType", rateType);
                map.put("graininess", graininess);
            }else {
                map.put("FlatValue", FlatValue.toString());
                map.put("time", time);
                map.put("rateType", rateType);
                map.put("graininess", graininess);
            }
        }

        //谷
        if (s.equals(id)){
            if (map.containsKey("ValleyValue") || map.get("ValleyValue").equals(0)){
                map.replace("ValleyValue",ValleyValue.toString());
                map.replace("time",time);
                map.put("rateType", rateType);
                map.put("graininess", graininess);
            }else {
                map.put("ValleyValue", ValleyValue.toString());
                map.put("time", time);
                map.put("rateType", rateType);
                map.put("graininess", graininess);
            }
        }


        List<Map<String, Object>> list =(List<Map<String, Object>> ) map.get("children");
        for (Map<String, Object> stringMap : list) {
            Tree(stringMap,id,num,price,rate,PeekValue,FlatValue,ValleyValue,time,rateType,graininess);
           }
    }

    public List<Map<String, Object>>  CrbonPwerTree(Map<String,Double> collectPower,EnergyDTO energyDTO){
        //注入bean
        ObjMoudelService bean = SpringUtils.getBean(ObjMoudelService.class);
        List<Map<String, Object>> listAll = new ArrayList<>();
        //拿到所有时间段的能耗值以及id 和 parentId

            //拿到注入bean的树的方法
            Map<String, Object> mapAllTree = bean.selectPowerTree(energyDTO);

        for (Map.Entry<String, Double> stringDoubleEntry : collectPower.entrySet()) {
            CrbonTree(mapAllTree, stringDoubleEntry.getKey(),stringDoubleEntry.getValue());

        }
        listAll.add(mapAllTree);
        return  listAll;
    }
    public void  CrbonTree(Map<String,Object> map, String id,Double crobon){
        String s =  (String) map.get("id");
        if (s.equals(id)){
            map.put("crobon", crobon.toString());
        }

        List<Map<String, Object>> list =(List<Map<String, Object>> ) map.get("children");
        for (Map<String, Object> stringMap : list) {
            CrbonTree(stringMap,id,crobon);
        }
    }

}